package com.ruoyi.business.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * @Author: ygw
 * @Date: 2022/09/28/18:14
 * @Description:
 */
@RestController
@RequestMapping("/pdl/config")
public class PdlConfigController {

    @Autowired
    private RedisCache redisCache;
    /**
     * 业务系统 清空缓存
     */
    @DeleteMapping("/clearCache")
    public AjaxResult clearDictCache() {
        Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");
        redisCache.deleteObject(keys);
        return AjaxResult.success();
    }

    /**
     * 设置缓存
     * @param
     * @return
     */
    @PostMapping("/setCache")
    public AjaxResult setCache (@RequestBody SysConfig config) {
        redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        return AjaxResult.success();
    }

    /**
     * 设置cache key
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey) {
        return Constants.SYS_CONFIG_KEY + configKey;
    }

}
